xquery version "1.0-ml";

import module namespace search = "http://marklogic.com/appservices/search" at "/MarkLogic/appservices/search/search.xqy";
import module namespace plib = "http://porvino.com/lib" at "/porvino/lib/lib.xqy";

(:	We do not need the id's of the names here because we are only returning a list
	of names to the wine form to make the data entry easier. Not using the id of any wines
:)

let $inputterm := xdmp:get-request-field("term")
let $searchterm :=
    if ($inputterm = "")
    then ""
    else
    for $t in fn:tokenize($inputterm, " ")
    return fn:concat("*", $t, "*") 
(:
let $searchterm := fn:concat("*", $inputterm, "*")
let $wines := cts:search(doc()//wine, cts:word-query($searchterm, ("wildcarded", "case-insensitive")))
:)
let $options :=
    <options xmlns="http://marklogic.com/appservices/search">
      <searchable-expression>//wine</searchable-expression>
      <term>
        <empty apply="no-results" />
        <term-option>wildcarded</term-option>
        <term-option>case-insensitive</term-option>
      </term>
      <return-results>true</return-results>
      <transform-results apply="search-result-transform" ns="http://porvino.com/lib" at="/porvino/lib/lib.xqy"></transform-results>
    </options>

let $wines := search:search($searchterm, $options, 1, 100)//search:result/wine

return

<html xmlns="http://www.w3.org/1999/xhtml">
	<head>
	<meta http-equiv="content-type" content="text/html; charset=UTF-8"></meta>
	<title></title>
	<script type="text/javascript" src="list/listTableDebug.js"></script>
	<link rel="stylesheet" href="/porvino/style/home.css" type="text/css"></link>
	
	<!-- script and style for table -->
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script> 
    <script type="text/javascript" src="http://tablesorter.com/jquery.tablesorter.min.js"></script>
    <script type="text/javascript" id="js">
            <![CDATA[$(document).ready(function() {// call the tablesorter plugin
            $("#wineList").tablesorter( {sortList: [[0,0]], widgets:['zebra']} );});]]>
    </script>
    <link rel="stylesheet" href="/porvino/style/tableSorter/style.css" type="text/css" media="print, projection, screen" />
    
	</head>
	<body>
	   {plib:globalNav($inputterm)}
       {plib:listWines($wines)}
	</body>
</html>